home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group97a.txt / 000120_icon-group-sender _Mon Apr 21 10:25:00 1997.msg < prev    next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Received: from kingfisher.CS.Arizona.EDU by cheltenham.cs.arizona.edu; Mon, 21 Apr 1997 13:23:14 MST
  2. Received: by kingfisher.CS.Arizona.EDU; (5.65v3.2/1.1.8.2/08Nov94-0446PM)
  3.     id AA05093; Mon, 21 Apr 1997 13:23:14 -0700
  4. Date: Mon, 21 Apr 1997 10:25:00 -0700
  5. From: swampler@noao.edu (Steve Wampler)
  6. Subject: Re: Comments on Icon Routine
  7. To: icon-group@cs.arizona.edu
  8. Message-Id: <swampler-9703211724.AA003615590@orpheus.gemini.edu>
  9. In-Reply-To: <3358C3DD.23EE@airmail.net>
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11. Status: RO
  12. Content-Length: 2322
  13.  
  14.  
  15.  
  16. Nathanael Graham wrote:
  17. > To fellow Iconers:
  18. >    This week at work I wanted to take documents created by MS-Word or 
  19. > WordPerfect and break down the 80+ character lines into lines of no more 
  20. > than 80 characters and I figured that this would be a perfect use for 
  21. > Icon.  The routine did not take too long to code and, at the suggestion 
  22. > of a fellow worker, I generalized it to allow for lines of any specified 
  23. > length.  Assuming the Icon routine is called truncate.icn/truncate.exe,
  24. > one would type the following on the command line:
  25. >     truncate infile.txt outfile.txt 80
  26. > This would:
  27. >     . Read in each record from infile.txt
  28. >     . After deleting leading spaces, it would write the record to       
  29. >       outfile.txt, if the record length was 80 or less
  30. >     . If the record length was greater than 80, then it would "break up" 
  31. >       the record on spaces, scanning for them from position 81 and going 
  32. >       backwards.  Once it found the space, it would write from the      
  33. >       beginning of the record to the space and repeat the same          
  34. >       algorithm starting with what was left of the record after the     
  35. >       space.  If no space was found in any record or piece thereof of   
  36. >       length 80 or more, then the first 80 characters would be written, 
  37. >       and the rest of the record would be handled per above.
  38.  
  39. I suspect you're going to see a lot of different ways to do this!  Here's one
  40. off the top of my head - I'm sure there are better ways.
  41.  
  42. procedure main(args)
  43.  
  44.     in  := open(args[1], "r") | stop("Cannot open ", args[1])
  45.     out := open(args[2], "w") | stop("Cannot open ", args[2])
  46.     rmargin := args[3] | 80
  47.  
  48.     while produce(out, read(in), rmargin)
  49.  
  50. end
  51.  
  52. procedure produce(f, s, rm)
  53.  
  54.     s := ltrim(s)
  55.     if *s <= rm
  56.         then write(f, s)
  57.         else {
  58.             reverse(s[1+:rm]) ? {
  59.                  suffix := reverse(tab(upto(' \t'))) | ""
  60.                  write(f, reverse(tab(0)))
  61.                  produce(f, suffix||s[rm+1:0], rm)
  62.                  }
  63.             }
  64.  
  65.     return
  66. end
  67.  
  68. procedure ltrim(s)
  69.     return s ?:= (tab(many(' ')), tab(0))
  70. end
  71.  
  72. --
  73. Steve Wampler - swampler@gemini.edu [Gemini 8m Telescopes Project (under AURA)]
  74. O Sibile, si ergo, fortibus es inero.
  75. Nobile, demis trux.  Demis phulla causan dux.
  76.